package com.probiz.estoremf.cart.web.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;

import com.probiz.estore.cart.util.ShoppingCartUtil;
import com.probiz.estore.common.model.cart.Shoppingcart;
import com.probiz.estore.common.model.customer.Favorite;
import com.probiz.estore.common.model.sales.PromoRule;
import com.probiz.estore.common.service.CheckoutService;
import com.probiz.estore.common.service.PromoService;
import com.probiz.estore.core.controller.GenericStoreFrontController;
import com.probiz.estore.customer.service.FavoriteManager;
import com.probiz.estore.exception.OutOfStockException;
import com.probiz.estore.webapp.util.RequestContext;

public class ShoppingcartController extends GenericStoreFrontController {

	private FavoriteManager favoriteManager = null;
	private PromoService promoService;
	private CheckoutService checkoutService;
	
	public void setCheckoutService(CheckoutService checkoutService) {
		this.checkoutService = checkoutService;
	}

	public void setPromoService(PromoService promoService) {
		this.promoService = promoService;
	}

	public void setFavoriteManager(FavoriteManager favoriteManager) {
		this.favoriteManager = favoriteManager;
	}
	
	@Override
	protected void initController() throws Exception {
	}

	public ModelAndView defaultAction(HttpServletRequest request, HttpServletResponse response) {
		if (logger.isDebugEnabled()) {
			logger.debug("entering 'ShoppingcartController defaultAction' method...");
		}

		// 取出购物车
		Shoppingcart shoppingcart = ShoppingCartUtil.getInstance().getCurrentUserShoppingcart();
		request.setAttribute("shoppingcart", shoppingcart);

		// 取出是否缺货
		try {
			shoppingcart.checkInventory();
		} catch (OutOfStockException outOfStockException) {
			request.setAttribute("outOfStockException", outOfStockException);
		}

		//验证购物车数据是否被删除
		shoppingcart = checkoutService.validationDeleted(request, response, shoppingcart);
		request.setAttribute("shoppingcart", shoppingcart);
		
		// 取出赠送的优惠券促销活动
		PromoRule promoRule = promoService.getCouponType(shoppingcart.getGainedCouponTypeId());
		request.setAttribute("coupon", promoRule);

		// 取出收藏夹
		if (!RequestContext.isAnonymousUser()) {
			List<Favorite> favoriteList = favoriteManager.getFavoritesByCustomer(RequestContext.getCurrentUserId(), 10);
			request.setAttribute("favoriteList", favoriteList);
		}

		// 把购物车里产品的ID传递给JSP页处理
		String sourceIds = shoppingcart.getProductIds();
		request.setAttribute("sourceIds", sourceIds);
		
		ModelAndView mv = new ModelAndView("cart/shoppingcart");

		return mv;
	}

	public ModelAndView refreshBill(HttpServletRequest request, HttpServletResponse response) throws ServletException {
		Shoppingcart shoppingcart = ShoppingCartUtil.getInstance().getCurrentUserShoppingcart();
		request.setAttribute("shoppingcart", shoppingcart);

		PromoRule promoRule = promoService.getCouponType(shoppingcart.getGainedCouponTypeId());
		request.setAttribute("coupon", promoRule);
		
		// 取出是否缺货
		try {
			shoppingcart.checkInventory();
		} catch (OutOfStockException outOfStockException) {
			request.setAttribute("outOfStockException", outOfStockException);
		}

		return new ModelAndView("cart/include/shoppingcartBill");
	}

	public ModelAndView refreshItems(HttpServletRequest request, HttpServletResponse response) throws ServletException {
		Shoppingcart shoppingcart = ShoppingCartUtil.getInstance().getCurrentUserShoppingcart();
		request.setAttribute("shoppingcart", shoppingcart);
		return new ModelAndView("cart/include/shoppingcartItems");
	}
	
	public ModelAndView refreshShoppingcartFavorite(HttpServletRequest request, HttpServletResponse response) throws ServletException {
		Shoppingcart shoppingcart = ShoppingCartUtil.getInstance().getCurrentUserShoppingcart();
		request.setAttribute("shoppingcart", shoppingcart);
		
		if (!RequestContext.isAnonymousUser()) {
			List<Favorite> favoriteList = favoriteManager.getFavorites(RequestContext.getCurrentUserId());
			request.setAttribute("favoriteList", favoriteList);
		}
		return new ModelAndView("cart/include/shoppingcartFavorite");
	}

	public ModelAndView refreshMinicart(HttpServletRequest request, HttpServletResponse response) throws ServletException {
		Shoppingcart shoppingcart = ShoppingCartUtil.getInstance().getCurrentUserShoppingcart();
		request.setAttribute("shoppingcart", shoppingcart);
		return new ModelAndView("cart/divMiniCartBrief");
	}

}